package net

import (
	"context"
	"fmt"
	"os"

	"github.com/gogf/gf/v2"
	"github.com/gogf/gf/v2/frame/g"
)

func NewHelper() *helper {
	return &helper{
		ctx: context.Background(),
	}
}

type helper struct {
	ctx       context.Context
	userAgent string
}

func (r *helper) GetUserAgent() string {
	if r.userAgent == "" {
		name := g.Config().MustGet(r.ctx, "backend.Name", "").String()
		env := g.Config().MustGet(r.ctx, "backend.Env", "").String()
		hostname, _ := os.Hostname()

		r.userAgent = fmt.Sprintf(`GClient %s at %s;%s/%s`, gf.VERSION, hostname, name, env)
	}

	return r.userAgent
}
